//package com.bookcity.front.webfront.security;
//
//import com.bookcity.front.webfront.config.MyStore;
//import com.bookcity.front.webfront.vo.MemberService;
//import com.ruoyi.common.utils.StringUtils;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//
///**
// * @author oldliu
// * @since 1.0
// */
//public class DouyinAuthProvider implements AuthenticationProvider {
//
//    private MyStore redisTemplate;
//
//    public void setRedisTemplate(MyStore redisTemplate) {
//        this.redisTemplate = redisTemplate;
//    }
//
//    //private UserDetailsService userDetailsService;
//   // private MemberService memberService;
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        //  校验验证码
//        additionalAuthenticationChecks((DouyinToken) authentication);
//
//        //  校验手机号
//        String mobile = authentication.getPrincipal().toString();
//
//        //UserDetails userDetails = memberService.loadByDouyinid(mobile);
//         UserDetails userDetails = (UserDetails) redisTemplate.get(mobile);
//
//        if (null == userDetails) {
//            throw new BadCredentialsException("id不存在");
//        }
//
//        //  创建认证成功的Authentication对象
//        DouyinToken result = new DouyinToken(userDetails, userDetails.getAuthorities());
//        result.setDetails(authentication.getDetails());
//
//        return result;
//    }
//
//    private void additionalAuthenticationChecks(DouyinToken authentication) {
//        if (authentication.getCredentials() == null) {
//            throw new BadCredentialsException("id不能为空");
//        }
//       /// String mobile = authentication.getPrincipal().toString();
//        String did = authentication.getCredentials().toString();
//
//        //  从Session或者Redis中获取相应的验证码
//        //String smsCodeInSessionKey = "SMS_CODE_" + mobile;
//        //        String verificationCode = sessionStrategy.getAttribute(servletWebRequest, smsCodeInSessionKey);
//        //        String verificationCode = stringRedisTemplate.opsForValue().get(smsCodeInSessionKey);
//       // String verificationCode = "1234";
//
//        if (StringUtils.isBlank(did)) {
//            throw new BadCredentialsException("id不存在，请重新发送！");
//        }
//        if(redisTemplate.get(did)==null){
//            throw new BadCredentialsException("这个id不存在！");
//        }
//
////        if (!smsCode.equalsIgnoreCase(verificationCode)) {
////            throw new BadCredentialsException("验证码错误！");
////        }
//
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//         return (DouyinToken.class.isAssignableFrom(authentication));
//    }
//}
